package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BusinessInformDao;
import com.qc.entity.BusinessInformEntity;
import com.qc.entity.MerchantEntity;

@SuppressWarnings({ "unchecked","rawtypes" })
@Repository("businessInformDao")
public class BusinessInformDaoImpl implements BusinessInformDao{

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListBusinessInformCount(String title) {
        
        String sql = "select count(*) from t_business_inform where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(title) && !"undefined".equals(title) ) {
            sql += " and title like '%"+title+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List<BusinessInformEntity> getAllBusinessInform(String title,int pageNo, int pageSize) {
    	
        return null;
    }

    @Override
    public BusinessInformEntity getBusinessInformId(long bId) {
        
        String sql = "select a.b_id , a.title,a.content,a.image,a.created_time ,b.shop_name from t_business_inform a "
                + " left join t_merchant b on b.shop_id = a.shop_id "
                + " where 1 = 1 and a.if_del = '0' and a.shop_id = '"+bId+"'";
        BusinessInformEntity informEntity = new BusinessInformEntity();
        try {
            informEntity = (BusinessInformEntity)jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BusinessInformEntity.class));
        } catch (DataAccessException e) {
        }
        return informEntity;
    }

    @Override
    public int saveBusinessInform(BusinessInformEntity businessInform) {
        
        String sql = "insert into t_business_inform(shop_id,title,content,image) values(?,?,?,?)";
        
        return jdbcTemplate.update(sql,new Object[]{
                businessInform.getShopId(),businessInform.getTitle(),businessInform.getContent(),businessInform.getImage()});
    }

    @Override
    public int deleteBusinessInformId(long id) {
    	
    	String sql = "update t_business_inform set if_del = 1 where b_id = " + id;
        
    	return jdbcTemplate.update(sql);
    }

    @Override
    public int updateBusinessInformId(BusinessInformEntity businessInform) {

        String sql = "update t_business_inform set title = ? ,content =? , image = ? where b_id = ?";
        
        return jdbcTemplate.update(sql,new Object[]{
                businessInform.getTitle(),businessInform.getContent(),
                businessInform.getImage(),businessInform.getbId()});
    }

	@Override
	public int getListWebBusinessInformCount(String shopName, String time) {
		
		String sql = "select count(*) from t_business_inform a "
				+ " left join t_merchant b on b.shop_id = a.shop_id"
				+ " where 1 = 1 and a.if_del = '0' ";
        
	        if (StringUtil.isNotEmpty(shopName) && !"undefined".equals(shopName) ) {
	            sql += " and b.shop_name like '%"+shopName+"%' ";
	        }
	        
	        if (StringUtil.isNotEmpty(time) && !"undefined".equals(time) ) {
	            sql += " and a.created_time like '%"+time+"%' ";
	        }
	        
	        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<BusinessInformEntity> getAllWebBusinessInform(String shopName, String time, int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
		
		String sql = "select a.b_id,a.shop_id,"
				+ " a.title,"
				+ " b.shop_name,"
				+ " IFNULL(b.browse_number,0) browseNumber,"
				+ " a.created_time "
				+ " from t_business_inform a "
				+ " left join t_merchant b on b.shop_id = a.shop_id"
				+ " where 1 = 1 and a.if_del = '0' ";
		
			if (StringUtil.isNotEmpty(shopName) && !"undefined".equals(shopName) ) {
	            sql += " and b.shop_name like '%"+shopName+"%' ";
	        }
	        
	        if (StringUtil.isNotEmpty(time) && !"undefined".equals(time) ) {
	            sql += " and a.created_time like '%"+time+"%' ";
	        }
			sql += " order by a.created_time DESC ";
			sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
			
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper(MerchantEntity.class));
	}
	
	@Override
    public BusinessInformEntity getWebBusinessInformId(String bId) {
        
        String sql = "select a.b_id , a.title,a.content,a.image,a.created_time ,b.shop_name from t_business_inform a "
                + " left join t_merchant b on b.shop_id = a.shop_id "
                + " where 1 = 1 and a.if_del = '0' and a.b_id = '"+bId+"'";
        BusinessInformEntity informEntity = new BusinessInformEntity();
        try {
            informEntity = (BusinessInformEntity)jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BusinessInformEntity.class));
        } catch (DataAccessException e) {
        }
        return informEntity;
    }

}
